//
//  PlaceholderView.m
//  EPTool
//
//  Created by shiqing guo on 2018/5/8.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "PlaceholderView.h"
#import "EPButton.h"
#import "UIView+DDAddition.h"
#import "Macro.h"

@interface PlaceholderView ()
@property (nonatomic, retain) UIImageView *igvHint;
@property (nonatomic, retain) UILabel *lblHint;
@property (nonatomic, retain) UIButton *btnHintOperate;
@end

@implementation PlaceholderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupAllSubView];
    }
    
    return self;
}

- (void)setupAllSubView {
    
    self.backgroundColor = [UIColor whiteColor];
    
    //    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, IPHONE6_RETINAPX(28.0f))];
    //    [topView setBackgroundColor:COLOR_BK];
    //    [self addSubview:topView];
    
    self.igvHint = [[UIImageView alloc] initWithFrame:CGRectMake(0, 84.0f, 190.0f, 137.0f)];
    self.igvHint.centerX = self.width/2;
    [self addSubview:self.igvHint];
    
    self.lblHint = [[UILabel alloc] initWithFrame:CGRectMake(0, self.igvHint.bottom + 10.0f, 330.0f, 78.0f)];
    self.lblHint.backgroundColor = [UIColor clearColor];
    self.lblHint.font = [UIFont systemFontOfSize:14.0f];
    self.lblHint.numberOfLines = 0;
    self.lblHint.textAlignment = NSTextAlignmentCenter;
    self.lblHint.textColor = [EPColor epColor_descMessageGray];
    self.lblHint.centerX = self.width/2;
    [self addSubview:self.lblHint];
    
    self.btnHintOperate = [EPButton buttonWithFrame:CGRectMake(0, self.lblHint.bottom + 32.0f, 226.0f, 34.0f) style:EPBasicButton_btn2 target:self selector:@selector(onClickHintOperateBtnAction:)];
    self.btnHintOperate.centerX = self.width/2;
    [self.btnHintOperate setBackgroundColor:[EPColor epColor_Blue]];
    [self.btnHintOperate setTitleColor:[EPColor epColor_White] forState:UIControlStateNormal];
    self.btnHintOperate.titleLabel.font = SystemFont(14.0f);
    self.btnHintOperate.layer.cornerRadius = 17.0f;
    self.btnHintOperate.layer.masksToBounds = YES;
    [self addSubview:self.btnHintOperate];
}

- (void)onClickHintOperateBtnAction:(EPButton *)sender {
    if (_block) {
        _block();
    }
}

- (void)setHintImageName:(NSString *)hintImageName {
    _hintImageName = hintImageName;
    
    UIImage *image = [UIImage imageNamed:hintImageName];
    self.igvHint.image = image;
    if (!CGSizeEqualToSize(image.size, CGSizeZero)) {
        self.igvHint.width = image.size.width;
        self.igvHint.height = image.size.height;
        self.igvHint.centerX = self.centerX;
        self.lblHint.top = self.igvHint.bottom + 10.0f;
        self.btnHintOperate.top = self.lblHint.bottom + 30.0f;
    }
}

- (void)setHintText:(NSString *)hintText {
    _hintText = hintText;
    
    self.lblHint.text = hintText;
    if (hintText) {
        
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:8];
        [paragraphStyle1 setAlignment:NSTextAlignmentCenter];
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:self.lblHint.font,NSFontAttributeName,self.lblHint.textColor,NSForegroundColorAttributeName,paragraphStyle1,NSParagraphStyleAttributeName, nil];
        
        CGRect rect = [hintText boundingRectWithSize:CGSizeMake(self.lblHint.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
        
        NSMutableAttributedString *attributedString =  [[NSMutableAttributedString alloc] initWithString:hintText attributes:dic];
        _lblHint.attributedText = attributedString;
        
        self.lblHint.height = MAX(38.0f, rect.size.height);
        self.btnHintOperate.top = self.lblHint.bottom + 32.0f;
    }
}

- (void)setAttributedHintText:(NSAttributedString *)attributedHintText {
    _attributedHintText = attributedHintText;
    
    self.lblHint.attributedText = attributedHintText;
    if (_attributedHintText) {
        CGRect rect = [_attributedHintText boundingRectWithSize:CGSizeMake(self.lblHint.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
        
        self.lblHint.height = MAX(38.0f, rect.size.height);
        self.btnHintOperate.top = self.lblHint.bottom + 32.0f;
    }
}

- (void)setHintOperateBtnName:(NSString *)hintOperateBtnName {
    _hintOperateBtnName = hintOperateBtnName;
    
    if (_hintOperateBtnName == nil) {
        self.btnHintOperate.hidden = YES;
    } else {
        self.btnHintOperate.hidden = NO;
        
        CGRect rect = [_hintOperateBtnName boundingRectWithSize:CGSizeMake(MAXFLOAT, self.btnHintOperate.titleLabel.font.pointSize) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.btnHintOperate.titleLabel.font,NSFontAttributeName,[EPColor epColor_Blue],NSForegroundColorAttributeName, nil] context:nil];
        
        self.btnHintOperate.width = MIN(rect.size.width + 20.0f * 2, 330.0f);
        self.btnHintOperate.centerX = self.width/2;
        
        
        [self.btnHintOperate setTitle:_hintOperateBtnName forState:UIControlStateNormal];
    }
}
                                        
@end
